package huffman;

import java.util.*;

public class HuffmanEncoder {


    public static void main(String[] args) {


        HashSet<byte[]> byteHash = new HashSet<>();

        byteHash.add(new byte[]{0x00, 0x00, 0x01, 0x02});
        byteHash.add(new byte[]{0x00, 0x00, 0x01, 0x03});
        byteHash.add(new byte[]{0x00, 0x00, 0x01, 0x03});


        PriorityQueue<HuffNode> queue = new PriorityQueue<>(new Comparator<HuffNode>() {
            @Override
            public int compare(HuffNode o1, HuffNode o2) {
                return o1.value - o2.value;
            }
        });


        queue.add(new HuffNode(2));
        queue.add(new HuffNode(3));
        queue.add(new HuffNode(6));
        queue.add(new HuffNode(8));
        queue.add(new HuffNode(9));

        System.out.println(Arrays.asList(queue.toArray()) + "--" + queue.peek());


        HuffNode top = null;

        while (!queue.isEmpty()) {

            HuffNode v1 = queue.poll();
            HuffNode v2 = queue.poll();
            HuffNode parentNode = new HuffNode(v1.value + v2.value);
            parentNode.left = v1;
            parentNode.right = v2;
            if (queue.isEmpty()) {
                top = parentNode;
                break;
            }
            queue.add(parentNode);
            System.out.println(Arrays.asList(queue.toArray()));

        }


        printCodeTable(top);
        System.out.println(top);


    }

    private static void printCodeTable(HuffNode root) {

        //求每个椰子节点的 编码
        Map<Integer, String> table = new HashMap<Integer, String>();



    }

    static class HuffNode {

        HuffNode left;
        HuffNode right;

        int value;

        boolean valid;

        public HuffNode(int value) {
            //mark the origin data
            valid = true;
            this.value = value;
        }

        @Override
        public String toString() {
            return String.valueOf(value);
        }
    }

}
